package com.game.logic.gift;

import akka.actor.ActorRef;
import com.coment.enums.PushType;
import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.gift.GiftEffectCfg;
import com.coment.dto.ApiNotify;
import com.coment.message.roles.StrengthAction;
import com.game.framework.ActorManager;
import com.game.framework.manager.core.ActorDaoAble;
import com.game.logic.fighting.actor.RolesActor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GiftActor extends ActorDaoAble {
    private final GiftEffectCfg giftEffectCfg;

    public GiftActor(String giftId) {
        this.giftEffectCfg = Tables.cfg.get_tbgifteffectcfg().get(giftId);
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(ApiNotify.class, this::handleApiNotify)
                .match(GiftMessage.class, this::handleGiftMessage)
                .build();
    }

    private void handleGiftMessage(GiftMessage giftMessage) {
        if (this.giftEffectCfg == null) return;
        if (this.giftEffectCfg.vit == 0) return;
        String rolesId = gameDao.findByIdMust(giftMessage.anchorInfoId).getUserInfoIdSeat().get(giftMessage.userInfoId);

        int vit = giftEffectCfg.vit * giftMessage.num;

        ActorManager.getActor(RolesActor.class, rolesId).tell(
                new StrengthAction(3, vit),
                ActorRef.noSender()
        );
    }


    public record GiftMessage(
            String gitId,
            Integer anchorInfoId,
            Integer userInfoId,
            int num
    ) {
    }


    /// 处理礼物推送信息
    public void handleApiNotify(ApiNotify apiNotify) {
        if (!apiNotify.getPushType().equals(PushType.gift.getValue())) return;
        this.self().tell(new GiftMessage(apiNotify.getGiftId(), apiNotify.getAnchorInfoId(),
                apiNotify.getUserInfo().getId(),
                apiNotify.getGiftCount()), ActorRef.noSender());
    }
}
